﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace UserData
{

    public class NGCountMgr
    {
        public delegate void Event_ReflushNGCount();
        public Event_ReflushNGCount OnTestReflushNG = null;
        string str_PathName = @"D:\HY System\_Machine\Config.NG项目数据统计.json";
        public Dictionary<string, Dictionary<string, int>> NGTatal = new Dictionary<string, Dictionary<string, int>>();
        private static object obj = new object();
        private static NGCountMgr NGDataMgr;
        public NGCountMgr()
        {
            Read();
        }
        public static NGCountMgr GetInstance()
        {
            if (NGDataMgr == null)
            {
                lock (obj)
                {
                    NGDataMgr = new NGCountMgr();
                    return NGDataMgr;
                }

            }
            else
                return NGDataMgr;
        }
        private int Read()
        {
            if (!File.Exists(str_PathName))
            {
                Save(str_PathName);
            }
            Object obs = AccessJosnSerializer.JsonToObject(str_PathName, typeof(Dictionary<string, Dictionary<string, int>>));
            if (obs != null)
            {
                try
                {
                    NGTatal = (Dictionary<string, Dictionary<string, int>>)obs;

                }
                catch (Exception)
                {
                    
                }
            }
            return 0;
        }
        private int Save(string str_Path)
        {
            bool a = AccessJosnSerializer.ObjectToJson(str_Path, NGTatal);
            return 0;

        }

        public void LoadNGType(string strName, string[] strType)
        {
            if(!NGTatal.ContainsKey(strName))
            {
                NGTatal.Add(strName, new Dictionary<string, int>());

            }
            string[] list_Type = NGTatal[strName].Keys.ToArray();
            foreach (var item in list_Type)
            {
                if(!strType.Contains(item))
                    NGTatal[strName].Remove(item);
            }

            foreach (var item in strType)
            {
                if (NGTatal[strName].ContainsKey(item))
                    continue;
                else
                    NGTatal[strName].Add(item, 0);
            }
        }
        public void AddNGCount(string strName, string strType,int n_Count=1)
        {
            if(NGTatal.ContainsKey(strName))
            {
                if (NGTatal[strName].ContainsKey(strType))
                {
                    NGTatal[strName][strType]++;
                }
                Save(str_PathName);
                if (OnTestReflushNG != null)
                    OnTestReflushNG();
            }
        }
        public void ClearNGCount()
        {
            string str_PathNameSave = @"D:\设备生产数据备份\NG项目数据统计.csv";
            if (!Directory.Exists(Directory.GetParent(str_PathNameSave).FullName))
            {
                Directory.CreateDirectory(Directory.GetParent(str_PathNameSave).FullName);
            }
            foreach (var item in NGTatal)
            {
                SaveAdditionalInfo info = new SaveAdditionalInfo();
                info.time_当前时间.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                info.str_Name.Value = item.Key;
                CSVHelper.Instance.SaveToCSVPath(str_PathNameSave, item.Value, info);
                if (NGTatal.ContainsKey(item.Key))
                {
                    string[] list_Type = NGTatal[item.Key].Keys.ToArray();
                    foreach (var itemValue in list_Type)
                    {
                        NGTatal[item.Key][itemValue] = 0;
                    }
                    Save(str_PathName);

                }

            }
            if (OnTestReflushNG != null)
                OnTestReflushNG();
        }

    }
}